#include "stdlib.h"
#include "keyboard.h"
#include "platform.h"
#include "camera.h"
#include "core.h"
#include "conf.h"
#include "gui.h"
#include "gui_draw.h"
#include "gui_batt.h"
//-------------------------------------------------------------------

static char osd_buf[32];

//-------------------------------------------------------------------
static long get_batt_average() {
    #define VOLTS_N         100
    static unsigned short   volts[VOLTS_N] = {0};
    static unsigned int     n = 0, rn = 0;
    static unsigned long    volt_aver = 0;

    volt_aver-=volts[n];
    volts[n]=(unsigned short)stat_get_vbatt();
    volt_aver+=volts[n];
    if (++n>rn) rn=n;
    if (n>=VOLTS_N) n=0;
    return volt_aver/rn;
}

//-------------------------------------------------------------------
unsigned long get_batt_perc() {
    unsigned long v;

    v = get_batt_average();
    if (v>conf.batt_volts_max) v=conf.batt_volts_max;
    if (v<conf.batt_volts_min) v=conf.batt_volts_min;
    return (v-conf.batt_volts_min)*100/(conf.batt_volts_max-conf.batt_volts_min);
}

//-------------------------------------------------------------------
static void gui_batt_draw_icon () {
    int perc = get_batt_perc();
	
    // define colors of bar: color1=frame, color2=below part of bar, color3=above part of bar

    // define green g1=darker, g2=dark, g3=bright
    int g1 = 0x25, g2 = 0x5F, g3 = 0x51;

    // define yellow y1=darker, y2=dark, y3=bright
    int y1 = 0x6F, y2 = 0x6F, y3 = 0x66;
    // exceptions for yellow
#if defined (CAMERA_a610)
    if((mode_get()&MODE_MASK) == MODE_REC){
        y1 = 0x9F;
        y2 = 0x9F;
        y3 = 0x99;
    }
#endif 	
    // define red r1=darker, r2=dark, r3=bright
    int r1 = 0x2F, r2 = 0x22, r3 = 0x26;

    // define grey under bar grey2=dright grey3=dark
    int grey2 = 0x13, grey3 = 0x33;
	
    // exceptions for all colors
#if defined (CAMERA_ixus70_sd1000) || defined (CAMERA_ixus75_sd750)
    g1 = 0x53, g2 = 0x55, g3 = 0x51;
    y1 = 0x36, y2 = 0x66, y3 = 0x61;
    r1 = 0x83, r2 = 0x88, r3 = 0x81;
    grey2 = 0x11, grey3 = 0x33;
#endif

#if defined (CAMERA_sx200is)
    if((mode_get()&MODE_MASK) == MODE_REC) {
        g1 = 0x68;
        g2 = 0x68;
        g3 = 0x60;
        y1 = 0x93;
        y2 = 0x8F;
        y3 = 0x87;
    }
    else {
        g1 = 0xA0;
        g2 = 0xA0;
        g3 = 0xA0;
        y1 = 0x6C;
        y2 = 0x6A;
        y3 = 0x66;   
    }
    r1 = 0x2C, r2 = 0x2C, r3 = 0x20;
    grey2 = 0x10, grey3 = 0x15;
#endif

    // set bar color depending percent
    color cl1 = (perc>50) ? g1 :(perc<=20) ? r1 : y1;
    color cl2 = (perc>50) ? g2 :(perc<=20) ? r2 : y2;
    color cl3 = (perc>50) ? g3 :(perc<=20) ? r3 : y3;

    // icon
    draw_round_rect(conf.batt_icon_pos.x,       conf.batt_icon_pos.y+3,   conf.batt_icon_pos.x+3,   conf.batt_icon_pos.y+9,   MAKE_COLOR(COLOR_GREY, COLOR_GREY));
    draw_rect(conf.batt_icon_pos.x+3,           conf.batt_icon_pos.y,     conf.batt_icon_pos.x+31,  conf.batt_icon_pos.y+12,  MAKE_COLOR(COLOR_GREY, COLOR_GREY));
    draw_filled_rect(conf.batt_icon_pos.x+1,    conf.batt_icon_pos.y+4,   conf.batt_icon_pos.x+2,   conf.batt_icon_pos.y+8,   MAKE_COLOR(y1, y1));
    draw_filled_rect(conf.batt_icon_pos.x+4,    conf.batt_icon_pos.y+6,   conf.batt_icon_pos.x+30,  conf.batt_icon_pos.y+11,  MAKE_COLOR(grey3, grey3));
    draw_filled_rect(conf.batt_icon_pos.x+4,    conf.batt_icon_pos.y+1,   conf.batt_icon_pos.x+30,  conf.batt_icon_pos.y+5,   MAKE_COLOR(grey2, grey2));
    // fill icon
    draw_rect(conf.batt_icon_pos.x+4,           conf.batt_icon_pos.y+2,   conf.batt_icon_pos.x+30,  conf.batt_icon_pos.y+10,  MAKE_COLOR(cl1, cl1));
    draw_filled_rect(conf.batt_icon_pos.x+29-(25*perc/100),    conf.batt_icon_pos.y+6,     conf.batt_icon_pos.x+29,   conf.batt_icon_pos.y+9,  MAKE_COLOR(cl2, cl2));
    draw_filled_rect(conf.batt_icon_pos.x+29-(25*perc/100),    conf.batt_icon_pos.y+3,     conf.batt_icon_pos.x+29,   conf.batt_icon_pos.y+5,  MAKE_COLOR(cl3, cl3));
}

//-------------------------------------------------------------------
static void gui_batt_draw_charge(){
    int perc = get_batt_perc();
    color cl = (perc<=20)?conf.osd_color_warn:conf.osd_color;
    sprintf(osd_buf, "%3d%%", perc);
    osd_buf[5]=0;
    draw_string(conf.batt_txt_pos.x, conf.batt_txt_pos.y, osd_buf, cl);
}


//-------------------------------------------------------------------
void gui_batt_draw_osd() {
    if (conf.batt_perc_show)
        gui_batt_draw_charge();
    
    if (conf.batt_icon_show)
        gui_batt_draw_icon();
}

//-------------------------------------------------------------------
